package com.base_data_handler.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Logger;

public class NetData {

	private static final Logger log = Logger.getLogger(NetData.class.getName());
	public static String getStringData(String netUrl){
			String json = null;
			URL url = null;
			try {
				url = new URL(netUrl);
				url.openConnection();
//				 log.info(url.openConnection().getContentType()+"字符编码"+url.openConnection().getContentEncoding());
				url.openConnection().setConnectTimeout(30*60*60*1000);
				url.openConnection().setReadTimeout(30*60*60*1000);
				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				// FileOutputStream fo=new FileOutputStream("airjson");
//				 log.info("获取数据大小……"+url.getFile().getBytes("utf-8").length);
				InputStream inputStream = url.openStream();
//				 log.info("获取数据大小……"+inputStream.available());
				byte[] data = new byte[2048*8];
				int len = 0;
				if (inputStream != null) {
					try {
						while ((len = inputStream.read(data)) != -1) {
							outputStream.write(data, 0, len);
							// fo.write(data, 0, len);
						}
						json = new String(outputStream.toByteArray(), "utf-8");
						// fo.close();
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}else {
					 log.info("获取失败"+url.getUserInfo());
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info("获取数据发生错误"+url.getUserInfo());
			}

			return json;
	}
}
